﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Project.Co_DataTransformationPrototype.Data
{
    public partial class DbaseDialogBox : Form
    {
        private DatabaseSettings dbaseSettings;

        public DbaseDialogBox()
        {
            InitializeComponent();
        }

        private void DbaseDialogBox_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DialogResult != System.Windows.Forms.DialogResult.OK)
                this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void chkIntegratedSecurity_CheckedChanged(object sender, EventArgs e)
        {
            bool blnVisibility;

            //blnVisibility = !chkIntegratedSecurity.Checked;
            if (chkIntegratedSecurity.Checked)
                blnVisibility = false;
            else
                blnVisibility = true;

            lblUserName.Visible = txtUserName.Visible = blnVisibility;
            lblPassword.Visible = txtPassword.Visible = blnVisibility;
        }
        
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (isValidSettingsInput())
            {
                dbaseSettings = DatabaseSettings.GetInstance();
                dbaseSettings.SaveDBConnectionSettings(txtServerName.Text.Trim(), txtDatabaseName.Text.Trim(), txtUserName.Text.Trim(), chkIntegratedSecurity.Checked);
                
                this.Hide();
            }
        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            if (isValidSettingsInput())
            {
                btnTestConnection.Enabled = false;
                dbaseSettings = DatabaseSettings.GetInstance();
                dbaseSettings.ConnectionStringBuilder(txtServerName.Text.Trim(), txtDatabaseName.Text.Trim(), chkIntegratedSecurity.Checked, txtUserName.Text.Trim(), txtPassword.Text.Trim());

                try
                {
                    // TODO: test connection to master to establish server connection
                    // TODO: test connection to specified database

                    SQLUtilities.testConnection(dbaseSettings.ConnectionString);
                    btnSave.Enabled = true;
                    MessageBox.Show("Database Connection Succeeded", "Testing Database Connection", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    // TODO: move ex.Message
                    MessageBox.Show("Database Connection Failed \nError: " + ex.Message, "Testing Database Connection", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                finally
                {
                    btnTestConnection.Enabled = true;
                }
            }
        }

        private bool isValidSettingsInput()
        {
            bool blnIsValid = false;

            // TODO: Validation implementation and focus-setting

            if (!string.IsNullOrEmpty(txtServerName.Text.Trim()) && !string.IsNullOrEmpty(txtDatabaseName.Text.Trim()))
            {
                if (chkIntegratedSecurity.Checked)
                    blnIsValid = true;
                else if (!string.IsNullOrEmpty(txtUserName.Text.Trim()) && !string.IsNullOrEmpty(txtPassword.Text.Trim()))
                    blnIsValid = true;
            }

            if (!blnIsValid)
                MessageBox.Show("User input was invalid.", "Testing Database Connection", MessageBoxButtons.OK, MessageBoxIcon.Warning); 

            return blnIsValid;
        }

    }
}
